package city.spring.modules.cardinfo.controller;

import city.spring.modules.cardinfo.entity.CardEmployeeSetEntity;
import city.spring.modules.cardinfo.service.CardEmployeeSetService;
import city.spring.modules.system.entity.OrganizationEntity;
import city.spring.modules.system.service.OrganizationService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：
 *
 * @author panxm
 * @date 2020-04-30 11:48:04
 */
@RestController
@RequestMapping("/card_employee_set")
public class CardEmployeeSetController extends ApiController {
    private final CardEmployeeSetService cardEmployeeSetService;
    private final OrganizationService organizationService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<CardEmployeeSetEntity, ?>> entityOrderFields;

    public CardEmployeeSetController(CardEmployeeSetService cardEmployeeSetService, OrganizationService organizationService) {
        this.cardEmployeeSetService = cardEmployeeSetService;
        this.organizationService = organizationService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(CardEmployeeSetEntity::getId);
        entityOrderFields.add(CardEmployeeSetEntity::getIsUpdate);
        entityOrderFields.add(CardEmployeeSetEntity::getDept);
        entityOrderFields.add(CardEmployeeSetEntity::getDuties);
        entityOrderFields.add(CardEmployeeSetEntity::getMoney);
    }

    /**
     * 获取职务duites
     * @return
     */
//    @GetMapping("duites")
//    public Object dutiesList() {
//        return success(CardEmployeeSetService.dutiesList());
//    }

    /**
     * 获取全部的 <strong></strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardEmployeeSetEntity entity) {
        LambdaQueryChainWrapper<CardEmployeeSetEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong></strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardEmployeeSetEntity entity) {
        LambdaQueryChainWrapper<CardEmployeeSetEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<CardEmployeeSetEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        for (CardEmployeeSetEntity record : page.getRecords()) {
            try {
                OrganizationEntity organizationEntity = organizationService.getById(record.getDept());
                record.setDeptName(organizationEntity.getTitle());
            } catch (Exception ignore) {
                record.setDept(null);
                record.setDeptName("< 找不到员工 >");
            }
        }
        for (CardEmployeeSetEntity record : page.getRecords()) {
            try {
                if (record.getIsUpdate() == 1) {
                    record.setMonthUpdate("是");
                }
                if (record.getIsUpdate() == 0) {
                    record.setMonthUpdate("否");
                }
            } catch (Exception ignore) {
                record.setIsUpdate(null);
                record.setMonthUpdate("< 找不到 >");
            }
        }
        return success(page);
    }

    /**
     * 获取一个 <strong></strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(cardEmployeeSetService.getById(id));
    }

    /**
     * 添加一个 <strong></strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody CardEmployeeSetEntity entity) {
        cardEmployeeSetService.saveCardEmployeeSet(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong></strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody CardEmployeeSetEntity entity) {
        entity.setId(id);
        cardEmployeeSetService.updateCardEmployeeSet(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong></strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        cardEmployeeSetService.deleteCardEmployeeSet(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong></strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        cardEmployeeSetService.deleteCardEmployeeSet(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<CardEmployeeSetEntity> buildLambdaQuery(CardEmployeeSetEntity entity) {
        LambdaQueryChainWrapper<CardEmployeeSetEntity> lambdaQuery = cardEmployeeSetService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), CardEmployeeSetEntity::getId, entity.getId());
        lambdaQuery.like(entity.getIsUpdate() != null, CardEmployeeSetEntity::getIsUpdate, entity.getIsUpdate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDept()), CardEmployeeSetEntity::getDept, entity.getDept());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDuties()), CardEmployeeSetEntity::getDuties, entity.getDuties());
        lambdaQuery.like(entity.getMoney() != null, CardEmployeeSetEntity::getMoney, entity.getMoney());
        return lambdaQuery;
    }

}
